<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="80px">
			<div class="app-space-between">
				<div class="app-space-between-left">
					<el-form-item label="门票类型:" prop="ticketType">
						<el-select v-model="queryParams.ticketType" @change="handleQuery" placeholder="请选择" clearable>
							<el-option v-for="dict in dict.type.product_type" :key="dict.value" :label="dict.label"
								:value="dict.value" />
						</el-select>
					</el-form-item>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column show-overflow-tooltip type="selection" min-width="55" align="center" />
			<el-table-column show-overflow-tooltip label="序号" type="index" min-width="55" align="center" />
			<el-table-column show-overflow-tooltip label="上下架" min-width="120" align="center">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.onShowType" :inactive-value="1" :active-value="0"
						active-color="rgb(19,34,122)" @change="e=>handleSwitch({e,data:scope.row})"
						inactive-color="#cccccc" />
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="门票类型" min-width="300" prop="ticketType">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.product_type" :value="scope.row.ticketType" />
				</template>
			</el-table-column>
			<el-table-column label="销售单价(元)" align="center" prop="sellingPrice" min-width="100" />
			<el-table-column show-overflow-tooltip label="入园时间" min-width="220" prop="enterTime">
				<template slot-scope="scope">
					<div>{{scope.row.enterTime}}</div>
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="入园地址" min-width="300" prop="enterAddress" />
			<el-table-column show-overflow-tooltip label="入园方式" min-width="200" prop="admissionMethod" />
			<el-table-column show-overflow-tooltip label="年卡使用提示" min-width="300" prop="annualCardTips" />
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改岗位对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
			<div style="padding-right: 40px;">
				<el-form ref="form" :model="form" :rules="rules" label-width="140px" v-loading="diLogLoading">
					<el-row>
						<el-col :span="24">
							<el-form-item label="门票类型:" prop="ticketType">
								<el-select v-model="form.ticketType" placeholder="请选择门票类型" clearable>
									<el-option v-for="dict in dict.type.product_type" :key="dict.value"
										:label="dict.label" :value="dict.value" />
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					
					<el-row>
						<el-col :span="24">
							<el-form-item label="销售单价（元）:" prop="sellingPrice">
								<el-input v-model="form.sellingPrice" placeholder="请输入销售单价"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="24">
							<el-form-item label="入园时间:" prop="enterTime">
								<el-input v-model="form.enterTime" placeholder="请输入入园时间"></el-input>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="24">
							<el-form-item label="入园地址:" prop="enterAddress">
								<el-input v-model="form.enterAddress" placeholder="请输入入园地址"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="24">
							<el-form-item label="入园方式:" prop="admissionMethod">
								<el-input v-model="form.admissionMethod" placeholder="请输入入园方式"></el-input>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="24">
							<el-form-item label="允许总人数:" prop="totalNumber">
								<el-input-number v-model="form.totalNumber" controls-position="right" :min="0"
									:max="9999999"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="24">
							<el-form-item label="上下架:" prop="onShowType">
								<el-switch v-model="form.onShowType" :inactive-value="1" :active-value="0"
									active-color="rgb(19,34,122)" inactive-color="#cccccc" />
							</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="24">
							<el-form-item label="允许儿童人数:" prop="childNumber">
								<el-input-number v-model="form.childNumber" controls-position="right" :min="0"
									:max="9999999"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="24">
							<el-form-item label="允许老年人数:" prop="elderNumber">
								<el-input-number v-model="form.elderNumber" controls-position="right" :min="0"
									:max="9999999"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row v-if="form.ticketType == '00001231_00014083'">
						<el-col :span="24">
							<el-form-item label="年卡使用提示:" prop="annualCardTips">
								<el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="form.annualCardTips" />
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<div slot="footer" class="dialog-footer" v-if="!diLogLoading">
				<el-button type="primary" @click="submitForm" :loading="btnLoading">{{form.id?'保 存':'确 定'}}
				</el-button>
				<el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import QuillEdit from "@/components/Editor";
	import {
		parametersDelete,
		parametersDetails,
		parametersEdit,
		parametersList,
		parametersAdd
	} from "@/api/scenicSpot/ticketParameters.js";
	QuillEdit
	export default {
		name: "ScenicSpotTicketParameters",
		dicts: ['product_type'],
		components: {
			QuillEdit
		},
		data() {
			return {
				// 遮罩层
				loading: true,
				btnLoading: false,
				ticketDicData: [{
						label: '成人票',
						value: '成人票'
					},
					{
						label: '儿童票',
						value: '儿童票'
					},
					{
						label: '双人登塔套票',
						value: '双人登塔套票'
					},
					{
						label: '亲子1+1票',
						value: '亲子1+1票'
					},
					{
						label: '亲子2+1票',
						value: '亲子2+1票'
					},
					{
						label: '沉浸式VR嗨玩套票（儿童票）',
						value: '沉浸式VR嗨玩套票（儿童票）'
					},
					{
						label: '沉浸式VR嗨玩套票（成人票）',
						value: '沉浸式VR嗨玩套票（成人票）'
					},
					{
						label: '年卡',
						value: '年卡'
					}
				],
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				diLogLoading: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					ticketType: "",
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					sellingPrice: {
						required: true,
						message: "请输入销售单价",
						trigger: "blur"
					},
					ticketType: {
						required: true,
						message: "请选择门票类型",
						trigger: "blur"
					},
					enterTime: {
						required: false,
						message: "请输入入圆时间",
						trigger: "blur"
					},
					enterAddress: {
						required: false,
						message: "请输入入园地址",
						trigger: "blur"
					},
					admissionMethod: {
						required: false,
						message: "请输入入园方式",
						trigger: "blur"
					},
					annualCardTips: {
						required: true,
						message: "请输入年卡使用提示",
						trigger: "blur"
					}
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				let form = {};
				Object.keys(this.queryParams).forEach(key => {
					if (this.queryParams[key]) form[key] = this.queryParams[key];
				});
				parametersList(form).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			async handleSwitch(options) { //上下架
				try {
					this.loading = true;
					options.data.onShowType = options.e;
					let res = await parametersEdit(options.data);
					this.getList();
				} catch (e) {
					console.error(e)
					this.loading = false;
				}
			},
			// 取消按钮
			cancel() {
				this.open = false;
			},
			// 表单重置
			reset() {
				this.form = {
					ticketType: '',
					enterTime: '',
					enterAddress: '',
					admissionMethod: '',
					annualCardTips: '',
					totalNumber: 0,
					onShowType: 0,
					sellingPrice: ""
					// childNumber: 0,
					// elderNumber: 0,
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.title = "新增";
				this.open = true;
				this.btnLoading = false;
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				let postId = row.id || this.ids
				if (Array.isArray(postId)) postId = postId.toString()
				this.title = "编辑";
				this.open = true;
				this.btnLoading = false;
				this.diLogLoading = true
				parametersDetails(postId).then(response => {
					for (let i in this.form) this.form[i] = response.data[i];
					this.form.id = postId
					this.diLogLoading = false
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						let form = JSON.parse(JSON.stringify(this.form));
						this.btnLoading = true;
						if (form.id != undefined) {
							parametersEdit(form).then(response => {
								this.$modal.msgSuccess("编辑成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						} else {
							parametersAdd(form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.id || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的内容，是否要继续？').then(function() {
					return parametersDelete(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('system/post/export', {
					...this.queryParams
				}, `post_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
